package com.chen.recruit.config;

import cn.dev33.satoken.interceptor.SaAnnotationInterceptor;
import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForStyle;
import cn.dev33.satoken.stp.StpLogic;
import com.chen.recruit.common.converter.EnumConverterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * sa-token配置类
 *
 * @author cgy
 * @date 2022/08/20
 */
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册Sa-Token的路由拦截器
        registry.addInterceptor(new SaRouteInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/system/user/login",
                        "/system/user/enterprise/register",
                        "/system/user/reset/password",
                        "/captcha",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/v2/**",
                        "/doc.html",
                        "/error",
                        "/favicon.ico"
                );
        // 开启注解鉴权
        registry.addInterceptor(new SaAnnotationInterceptor())
                .addPathPatterns("/**");
    }

    // 添加枚举转换
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new EnumConverterFactory());
    }

    @Bean
    public StpLogic getStpLogicJwt() {
        return new StpLogicJwtForStyle();
    }

}
